package com.cloudsigns.factorymanager2.produce.bean;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.utils.ParseJson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class WorkPartOrderBean implements Serializable {
    private int amount;// 工序分派的数量
    private String assignAccountId; // 派发人账号ID
    private String assignAccountName; // 派发人账号名称
    private String assignReceiveAccountId; // 接收人账号ID
    private String assignReceiveAccountName; // 接收人账号名称
    private CommonEnum.ProduceOrderStatus assignReceiveStatus; // 分派接收状态 必须为以下可用值之一: [ "CREATED", "EDIT", "EDITED", "SEND", "RECEIVED", "RETURNED", "COMPLETED" ]
    private String workPartOrderId;
    private long receiveAssignOrderTime; // 分派接收时间
    private long sendAssignOrderTime; // 分派发送时间
    private List<WorkPartDailyFeedbackBean> workPartDailyFeedbackList = new ArrayList<>();
    private List<WorkPartHandOverOrderBean> workPartHandOverOrderList = new ArrayList<>();

    /*上级id*/
    private String orderId;    //生产订单Id
    private String productId;//	产品Id
    private String workPartId;// 工序Id

    public void setParentId(String orderId, String productId, String workPartId) {
        this.orderId = orderId;
        this.productId = productId;
        this.workPartId = workPartId;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getAssignAccountId() {
        return assignAccountId;
    }

    public void setAssignAccountId(String assignAccountId) {
        this.assignAccountId = assignAccountId;
    }

    public String getAssignAccountName() {
        return assignAccountName;
    }

    public void setAssignAccountName(String assignAccountName) {
        this.assignAccountName = assignAccountName;
    }

    public String getAssignReceiveAccountId() {
        return assignReceiveAccountId;
    }

    public void setAssignReceiveAccountId(String assignReceiveAccountId) {
        this.assignReceiveAccountId = assignReceiveAccountId;
    }

    public String getAssignReceiveAccountName() {
        return assignReceiveAccountName;
    }

    public void setAssignReceiveAccountName(String assignReceiveAccountName) {
        this.assignReceiveAccountName = assignReceiveAccountName;
    }

    public CommonEnum.ProduceOrderStatus getAssignReceiveStatus() {
        return assignReceiveStatus;
    }

    public void setAssignReceiveStatus(CommonEnum.ProduceOrderStatus assignReceiveStatus) {
        this.assignReceiveStatus = assignReceiveStatus;
    }

    public String getWorkPartOrderId() {
        return workPartOrderId;
    }

    public void setWorkPartOrderId(String workPartOrderId) {
        this.workPartOrderId = workPartOrderId;
    }

    public long getReceiveAssignOrderTime() {
        return receiveAssignOrderTime;
    }

    public void setReceiveAssignOrderTime(long receiveAssignOrderTime) {
        this.receiveAssignOrderTime = receiveAssignOrderTime;
    }

    public long getSendAssignOrderTime() {
        return sendAssignOrderTime;
    }

    public void setSendAssignOrderTime(long sendAssignOrderTime) {
        this.sendAssignOrderTime = sendAssignOrderTime;
    }

    public List<WorkPartDailyFeedbackBean> getWorkPartDailyFeedbackList() {
        return workPartDailyFeedbackList;
    }

    public void setWorkPartDailyFeedbackList(List<WorkPartDailyFeedbackBean> workPartDailyFeedbackList) {
        this.workPartDailyFeedbackList = workPartDailyFeedbackList;
    }

    public List<WorkPartHandOverOrderBean> getWorkPartHandOverOrderList() {
        return workPartHandOverOrderList;
    }

    public void setWorkPartHandOverOrderList(List<WorkPartHandOverOrderBean> workPartHandOverOrderList) {
        this.workPartHandOverOrderList = workPartHandOverOrderList;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getWorkPartId() {
        return workPartId;
    }

    public void setWorkPartId(String workPartId) {
        this.workPartId = workPartId;
    }

    public void parseJson(JSONObject jsonObj) throws JSONException {
        if (jsonObj.has("amount")) {
            String amountStrwo = jsonObj.getString("amount");
            if (null != amountStrwo && !TextUtils.isEmpty(amountStrwo) && !"null".equals(amountStrwo)) {
                setAmount(Integer.parseInt(amountStrwo));
            }
        }
        if (jsonObj.has("assignAccountId")) {
            setAssignAccountId(jsonObj.getString("assignAccountId"));
        }
        if (jsonObj.has("assignAccountName")) {
            setAssignAccountName(jsonObj.getString("assignAccountName"));
        }
        if (jsonObj.has("assignReceiveAccountId")) {
            setAssignReceiveAccountId(jsonObj.getString("assignReceiveAccountId"));
        }
        if (jsonObj.has("assignReceiveAccountName")) {
            setAssignReceiveAccountName(jsonObj.getString("assignReceiveAccountName"));
        }
        if (jsonObj.has("assignReceiveStatus")) {
            CommonEnum.ProduceOrderStatus assignReceiveStatus = ParseJson.parseProdutOrderStatus(jsonObj, "assignReceiveStatus");
            setAssignReceiveStatus(assignReceiveStatus);
        }
        if (jsonObj.has("workPartOrderId")) {
            setWorkPartOrderId(jsonObj.getString("workPartOrderId"));
        }
        if (jsonObj.has("receiveAssignOrderTime")) {
            String receiveAssignOrderTimeStr = jsonObj.getString("receiveAssignOrderTime");
            if (!TextUtils.isEmpty(receiveAssignOrderTimeStr) && !receiveAssignOrderTimeStr.equals("null")) {
                setReceiveAssignOrderTime(Long.parseLong(receiveAssignOrderTimeStr));
            }
        }
        if (jsonObj.has("sendAssignOrderTime")) {
            String sendAssignOrderTimeStr = jsonObj.getString("sendAssignOrderTime");
            if (!TextUtils.isEmpty(sendAssignOrderTimeStr) && !sendAssignOrderTimeStr.equals("null")) {
                setSendAssignOrderTime(Long.parseLong(sendAssignOrderTimeStr));
            }
        }
        if (jsonObj.has("workPartDailyFeedbackList")) {
            String workPartDailyFeedbackListJsonArrStr = jsonObj.getString("workPartDailyFeedbackList");
            this.workPartDailyFeedbackList.clear();
            if (null != workPartDailyFeedbackListJsonArrStr && !workPartDailyFeedbackListJsonArrStr.equals("null") && !TextUtils.isEmpty(workPartDailyFeedbackListJsonArrStr)) {
                JSONArray workPartDailyFeedbackListJsonArr = new JSONArray(workPartDailyFeedbackListJsonArrStr);

                for (int x = 0; x < workPartDailyFeedbackListJsonArr.length(); x++) {
                    WorkPartDailyFeedbackBean workPartDailyFeedbackBean = new WorkPartDailyFeedbackBean();

                    workPartDailyFeedbackBean.setParnetId(orderId, productId, workPartId, workPartOrderId);
                    JSONObject workPartDailyFeedbackListJsonObj = (JSONObject) workPartDailyFeedbackListJsonArr.get(x);
                    workPartDailyFeedbackBean.parseJson(workPartDailyFeedbackListJsonObj);
                    this.workPartDailyFeedbackList.add(workPartDailyFeedbackBean);

                }
            }
        }
        if (jsonObj.has("workPartHandOverOrderList")) {
            String workPartHandOverOrderJsonArrStr = jsonObj.getString("workPartHandOverOrderList");
            workPartHandOverOrderList.clear();
            if (null != workPartHandOverOrderJsonArrStr && !workPartHandOverOrderJsonArrStr.equals("null") && !TextUtils.isEmpty(workPartHandOverOrderJsonArrStr)) {
                JSONArray workPartHandOverOrderListJsonArr = new JSONArray(workPartHandOverOrderJsonArrStr);
                for (int h = 0; h < workPartHandOverOrderListJsonArr.length(); h++) {
                    WorkPartHandOverOrderBean workPartHandOverOrderBean = new WorkPartHandOverOrderBean();
                    JSONObject workPartHandOverOrderListJsonObj = (JSONObject) workPartHandOverOrderListJsonArr.get(h);
                    workPartHandOverOrderBean.parseJson(workPartHandOverOrderListJsonObj);
                    workPartHandOverOrderList.add(workPartHandOverOrderBean);
                }
            }
        }
    }

    /**
     * 删除工序派工信息
     */
    public void resetDispatchInforDefault() {
        amount = 0;
        assignReceiveAccountId = "";
        assignReceiveAccountName = "";
    }
}
